// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
// SPDX-License-Identifier: Apache-2.0

package user_dao

import (
	"context"

	"gitcode.com/lvupclub/lvup-api/dao/common"
	"gitcode.com/lvupclub/proto-gen-go.git/src/user_msg_api"
	"gitcode.com/lvupclub/proto-gen-go.git/src/user_msg_type_api"
	"go.mongodb.org/mongo-driver/v2/bson"
	"go.mongodb.org/mongo-driver/v2/mongo"
	"go.mongodb.org/mongo-driver/v2/mongo/options"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/anypb"
)

type UserMsgInfoDbItem struct {
	MsgId     string `bson:"_id"`
	UserId    string `bson:"userId"`
	MsgData   []byte `bson:"msgData"`
	TimeStamp int64  `bson:"timeStamp"`
	HasRead   bool   `bson:"hasRead"`
}

func (item *UserMsgInfoDbItem) ToUserMsgInfo() (*user_msg_api.UserMsgInfo, error) {
	msg := &anypb.Any{}
	err := proto.Unmarshal(item.MsgData, msg)
	if err != nil {
		msg, err = anypb.New(&user_msg_type_api.UnkwownData{})
		if err != nil {
			return nil, err
		}
	}
	return &user_msg_api.UserMsgInfo{
		MsgId:     item.MsgId,
		MsgData:   msg,
		TimeStamp: item.TimeStamp,
		HasRead:   item.HasRead,
	}, nil
}

type _UserMsgInfoDao struct {
	mc *mongo.Collection
}

func newUserMsgInfoDao(db *mongo.Database) (*_UserMsgInfoDao, error) {
	mc := db.Collection(common.DB_NS_USER + "msg_info")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{

		Keys: bson.D{{"userId", 1}, {"timeStamp", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_UserMsgInfoDao{mc: mc}, nil
}

func (db *_UserMsgInfoDao) Insert(ctx context.Context, dbItem *UserMsgInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_UserMsgInfoDao) Exist(ctx context.Context, userId, msgId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": msgId, "userId": userId})
	return count > 0, err
}

func (db *_UserMsgInfoDao) Get(ctx context.Context, userId, msgId string) (*UserMsgInfoDbItem, error) {
	retItem := &UserMsgInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": msgId, "userId": userId}).Decode(retItem)
	return retItem, err
}

func (db *_UserMsgInfoDao) Remove(ctx context.Context, userId, msgId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": msgId, "userId": userId})
	return err
}

func (db *_UserMsgInfoDao) Count(ctx context.Context, userId string, filterByHasRead, hasRead bool) (uint32, error) {
	query := bson.M{"userId": userId}
	if filterByHasRead {
		query["hasRead"] = hasRead
	}
	count, err := db.mc.CountDocuments(ctx, query)
	return uint32(count), err
}

func (db *_UserMsgInfoDao) List(ctx context.Context, userId string, filterByHasRead, hasRead bool, offset, limit int64) ([]*UserMsgInfoDbItem, error) {
	query := bson.M{"userId": userId}
	if filterByHasRead {
		query["hasRead"] = hasRead
	}

	cursor, err := db.mc.Find(ctx, query, options.Find().SetSkip(offset).SetLimit(limit).SetSort(bson.M{"timeStamp": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*UserMsgInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_UserMsgInfoDao) SetHasRead(ctx context.Context, userId, msgId string) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": msgId, "userId": userId}, bson.M{"$set": bson.M{"hasRead": true}})
	return err
}
